package com.tdzx.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;

import com.tdzx.adapter.SitesAdapter;
import com.tdzx.adapter.TuanListAdapter;

public class PollListView extends ListView implements OnScrollListener {
	
	private int visibleLastIndex = 0;   //�?��的可视项索引  
    private int visibleItemCount;       // 当前窗口可见项�?�? 
    public static final int SITE = 1024;
    public static final int TUAN = 1025;

	public PollListView(Context context) {
		super(context);
	}

	public PollListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public PollListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnScrollListener(this);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
	/*	Log.i("aaa", firstVisibleItem + "--" + visibleItemCount + "--" + totalItemCount);
		if(firstVisibleItem + visibleItemCount == totalItemCount) {
			Log.i("aaa", firstVisibleItem + "--" + visibleItemCount + "--" + totalItemCount + "底部");
			//滑�?底部�?
			SitesAdapter adapter = (SitesAdapter) getAdapter();
			if(adapter != null) {
				if(adapter.getDatasSize() > adapter.getCount()) {
					int page = adapter.getCurrentPage();
					page++;
					adapter.setCurrentPage(page);
					adapter.notifyDataSetChanged();
				} else if(adapter.getDatasSize() == adapter.getCount()) {
					if(handler != null) {
						handler.sendEmptyMessage(SitesActivity.POLLLIST);
					}
				}
				
			}
			
		}*/
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = getAdapter().getCount() - 1;
//		int lastIndex = itemsLastIndex + 1;  //底部的loadmore
		if(scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == itemsLastIndex) {
//			SitesAdapter adapter = (SitesAdapter) getAdapter();
			HeaderViewListAdapter headerAdapter = (HeaderViewListAdapter) getAdapter();
			if(what == SITE) {
				
				SitesAdapter adapter = (SitesAdapter) headerAdapter.getWrappedAdapter();
				if(adapter != null) {
					int page = adapter.getCurrentPage();
					if(adapter.getDatasSize() > adapter.getCount()) {
						page++;
						adapter.setCurrentPage(page);
						adapter.notifyDataSetChanged();
					} else if(adapter.getDatasSize() == adapter.getCount()) {
						if(handler != null) {
							page++; 
							adapter.setCurrentPage(page);
							handler.sendEmptyMessage(50);
						}
						
					}
					
				}
				
			} else {
				
				TuanListAdapter adapter = (TuanListAdapter) headerAdapter.getWrappedAdapter();
				if(adapter != null) {
					int page = adapter.getCurrentPage();
					if(adapter.getDatasSize() > adapter.getCount()) {
						page++;
						adapter.setCurrentPage(page);
						adapter.notifyDataSetChanged();
					} else if(adapter.getDatasSize() == adapter.getCount()) {
						if(handler != null) {
							page++; 
							adapter.setCurrentPage(page);
							handler.sendEmptyMessage(50);
						}
						
					}
					
				}
			}
		}
	}

	Handler handler;
	int what;
	public void setHandler(Handler handler,int what) {
		this.handler = handler;
		this.what = what;
	}
	
	
}
